#include "videowidget.h"

VideoWidget::VideoWidget(QWidget *parent) :
    Phonon::VideoPlayer(Phonon::VideoCategory,parent)
{
    this->hide();
    refreshTime(500);
    seekForReload=0;
    connect(this->mediaObject(),SIGNAL(totalTimeChanged(qint64)),this,SIGNAL(totalTimeChanged(qint64)));
    connect(this->mediaObject(),SIGNAL(tick(qint64)),this,SIGNAL(position(qint64)));
    connect(this->mediaObject(),SIGNAL(seekableChanged(bool)),this,SIGNAL(seekableChanged(bool)));
    connect(this->mediaObject(),SIGNAL(stateChanged(Phonon::State,Phonon::State)),this,SLOT(onStateChanged(Phonon::State,Phonon::State)));

}

void VideoWidget::playlocalfile(QString file){
    m_localfile=file;
    qDebug()<<"open file:"<<m_localfile;
    this->play(QUrl(m_localfile));
}

void VideoWidget::reload(){
    seekForReload = this->currentTime();
    qDebug()<<"reload file:"<<m_localfile;
    this->play(QUrl(m_localfile));
    connect(this->mediaObject(),SIGNAL(seekableChanged(bool)),this,SLOT(onSeekableReload()));
}

void VideoWidget::refreshTime(int ms){
    tickInterval=ms;
    this->mediaObject()->setTickInterval(tickInterval);
}

void VideoWidget::onSeekableReload(){
    disconnect(this->mediaObject(),SIGNAL(seekableChanged(bool)),this,SLOT(onSeekableReload()));
    if(seekForReload==0)
        return;
    this->seek(seekForReload);
    seekForReload=0;
}

void VideoWidget::onStateChanged(Phonon::State oldState, Phonon::State newState){
    if(Phonon::PausedState==newState)
        emit(pauseStateRaise());
    else if(Phonon::PlayingState==newState)
        emit(playStateRaise());
    qDebug()<<"old state:"<< oldState<<" ==> "<<newState;

}


















void VideoWidget::hideEvent(QHideEvent *e){
    //qDebug()<< "vid hideEvent" ;
    emit(hideEventSglRedirect());
  //topWidget->hide();
}

void VideoWidget::showEvent ( QShowEvent * event ){
    //qDebug()<< "vid showEvent" ;
    emit(showEventSglRedirect());
}

void VideoWidget::focusInEvent(QFocusEvent *e){
    //qDebug()<< "vid focusInEvent" ;
    emit(focusInEventSglRedirect());
}

void VideoWidget::focusOutEvent(QFocusEvent *e){
    //qDebug()<< "vid focusOutEvent" ;
    emit(focusOutEventSglRedirect());
}

void VideoWidget::keyPressEvent(QKeyEvent *e){
     //qDebug()<<"vid event:"<< e->text()<<" key:"<<e->key();
     emit(keyPressEventSglRedirect(e));
}

void VideoWidget::resizeEvent( QResizeEvent *){
    emit(resizeEventSglRedirect());
}

void VideoWidget::moveEvent( QMoveEvent *){
    emit(moveEventSglRedirect());
}



